/**
 * 
 */

package com.chengyu.eyc.service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.chengyu.eyc.constant.EycConstants;
import com.chengyu.eyc.enums.CouponTypeEnum;
import com.chengyu.eyc.enums.PublishInfoStatusEnum;
import com.chengyu.eyc.mapper.EycAdInfoMapper;
import com.chengyu.eyc.mapper.EycCouponRuleMapper;
import com.chengyu.eyc.mapper.EycDataConfigMapper;
import com.chengyu.eyc.mapper.EycInspPriceRuleMapper;
import com.chengyu.eyc.mapper.EycInspRuleMapper;
import com.chengyu.eyc.mapper.EycInspStationMapper;
import com.chengyu.eyc.mapper.EycInspStationWorkdayMapper;
import com.chengyu.eyc.mapper.EycLogMapper;
import com.chengyu.eyc.mapper.EycOtherShopMapper;
import com.chengyu.eyc.mapper.EycParmPickupCarMapper;
import com.chengyu.eyc.mapper.EycPublicInfoMapper;
import com.chengyu.eyc.mapper.EycUserCouponMapper;
import com.chengyu.eyc.mapper.EycVehicleAvAddrMapper;
import com.chengyu.eyc.mapper.EycVehicleAvItemMapper;
import com.chengyu.eyc.mapper.EycVehicleAvMapper;
import com.chengyu.eyc.mapper.EycVehicleMaintQaMapper;
import com.chengyu.eyc.mapper.EycVerPersonMapper;
import com.chengyu.eyc.model.EycAdInfo;
import com.chengyu.eyc.model.EycCouponRule;
import com.chengyu.eyc.model.EycDataConfig;
import com.chengyu.eyc.model.EycInspPriceRule;
import com.chengyu.eyc.model.EycInspRule;
import com.chengyu.eyc.model.EycInspStation;
import com.chengyu.eyc.model.EycInspStationWorkday;
import com.chengyu.eyc.model.EycLog;
import com.chengyu.eyc.model.EycOtherShop;
import com.chengyu.eyc.model.EycParmPickupCar;
import com.chengyu.eyc.model.EycPublicInfo;
import com.chengyu.eyc.model.EycUserCoupon;
import com.chengyu.eyc.model.EycVehicleAv;
import com.chengyu.eyc.model.EycVehicleAvAddr;
import com.chengyu.eyc.model.EycVehicleAvItem;
import com.chengyu.eyc.model.EycVehicleMaintQa;
import com.chengyu.eyc.model.EycVerPerson;
import com.chengyu.eyc.utils.JWTUtil;
import com.chengyu.eyc.utils.SeqNumUtil;
import com.chengyu.eyc.utils.SpringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author kevin
 *
 */
@Slf4j
@Transactional
@Service
public class EycBaseServiceImpl implements EycBaseService {

    @Autowired
    EycAdInfoMapper eycAdInfoMapper;

    @Autowired
    EycCouponRuleMapper eycCouponRuleMapper;

    @Autowired
    EycUserCouponMapper eycUserCouponMapper;

    @Autowired
    EycDataConfigMapper eycDataConfigMapper;

    @Autowired
    EycInspRuleMapper eycInspRuleMapper;

    @Autowired
    EycPublicInfoMapper eycPublicInfoMapper;

    @Autowired
    EycInspPriceRuleMapper eycInspPriceRuleMapper;

    @Autowired
    EycLogMapper eycLogMapper;

    @Autowired
    EycInspStationMapper eycInspStationMapper;

    @Autowired
    EycOtherShopMapper eycOtherShopMapper;

    @Autowired
    EycInspStationWorkdayMapper eycInspStationWorkdayMapper;

    @Autowired
    EycVehicleMaintQaMapper eycVehicleMaintQaMapper;

    @Autowired
    EycVehicleAvMapper eycVehicleAvMapper;

    @Autowired
    EycVehicleAvItemMapper eycVehicleAvItemMapper;

    @Autowired
    EycVehicleAvAddrMapper eycVehicleAvAddrMapper;
    
    @Autowired
    EycParmPickupCarMapper eycParmPickupCarMapper;
    
    @Autowired
    EycVerPersonMapper eycVerPersonMapper;
    
    
    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchNumberOfCarPickupDaily()
     */
    @Override
    public EycParmPickupCar searchNumberOfCarPickupDaily() {
        Condition condition=new Condition(EycParmPickupCar.class);
        condition.createCriteria().andNotEqualTo("deleted", 1);
        List<EycParmPickupCar> eycParmPickupCars=this.eycParmPickupCarMapper.selectByCondition(condition);
        if(CollectionUtil.isNotEmpty(eycParmPickupCars)) {
            return eycParmPickupCars.get(0);
        }
        return null;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#deleteInspStation(com.chengyu.eyc.model.EycInspStation)
     */
    @Override
    public EycInspStation deleteInspStation(EycInspStation eycInspStation) {
        eycInspStation.setIsActive(Boolean.FALSE);
        this.eycInspStationMapper.updateByPrimaryKey(eycInspStation);
        return eycInspStation;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#getTableMapper(java.lang.Class)
     */
    @Override
    @SuppressWarnings("unchecked")
    public <E> ConditionMapper<E> getTableMapper(final Class<E> tableClazz) {
        if (tableClazz == null) {
            return null;
        }
        final String mapperNme = String.valueOf(StringUtils.uncapitalize(tableClazz.getSimpleName())) + "Mapper";
        return (ConditionMapper<E>)SpringUtil.getBean(mapperNme);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#getUserId()
     */
    @Override
    public String getUserId() {
        if (isLogin()) {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principal instanceof UserDetails) {
                return ((UserDetails)principal).getUsername();
            }
        } else {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes instanceof ServletRequestAttributes) {
                HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
                String token = request.getHeader(EycConstants.TOKEN);
                if (StrUtil.isNotEmpty(token)) {
                    Claims claims = JWTUtil.parseJWT(token);
                    return claims.getId();
                }
            }
        }
        return null;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#insertEycUserCoupon(com.chengyu.eyc.model.EycCoupon)
     */
    @Override
    public int insertEycUserCoupon(EycUserCoupon eycCoupon) {
        eycCoupon.setCouponNum(SeqNumUtil.genCouponNum());
        return this.eycUserCouponMapper.insert(eycCoupon);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#insertEycLog(com.chengyu.eyc.model.EycLog)
     */
    @ApiOperation(value = "插入操作日志")
    @Override
    public EycLog insertEycLog(EycLog eycLog) {
        this.eycLogMapper.insert(eycLog);
        return eycLog;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#insertEycPublicInfo(com.chengyu.eyc.model.EycPublicInfo)
     */
    @Override
    public EycPublicInfo insertEycPublicInfo(EycPublicInfo eycPublicInfo) {
        this.eycPublicInfoMapper.insert(eycPublicInfo);
        return eycPublicInfo;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#insertEycVehicleAv(com.chengyu.eyc.model.EycVehicleAv)
     */
    @Override
    public EycVehicleAv insertEycVehicleAv(EycVehicleAv eycVehicleAv) {
        eycVehicleAv.setAvDt(new Date());
        this.eycVehicleAvMapper.insert(eycVehicleAv);
        if (CollUtil.isNotEmpty(eycVehicleAv.getItems())) {
            for (EycVehicleAvItem eycVehicleAvItem : eycVehicleAv.getItems()) {
                eycVehicleAvItem.setVehicleAvId(eycVehicleAv.getVehicleAvId());
                eycVehicleAvItemMapper.insert(eycVehicleAvItem);
            }
        }
        return eycVehicleAv;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#insertOrUpdateInspStation(com.chengyu.eyc.model.EycInspStation)
     */
    @Override
    public EycInspStation insertOrUpdateInspStation(EycInspStation eycInspStation) {
        if (StrUtil.isBlank(eycInspStation.getInspStationId())) {
            this.eycInspStationMapper.insert(eycInspStation);
        } else {
            this.eycInspStationMapper.updateByPrimaryKey(eycInspStation);
        }
        return eycInspStation;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#insertOrUpdateVehicleAvAddr(com.chengyu.eyc.model.EycVehicleAvAddr)
     */
    @Override
    public EycVehicleAvAddr insertOrUpdateVehicleAvAddr(EycVehicleAvAddr eycVehicleAvAddr) {
        if (StrUtil.isBlank(eycVehicleAvAddr.getVehicleAvAddrUuid())) {
            this.eycVehicleAvAddrMapper.insert(eycVehicleAvAddr);
        } else {
            this.eycVehicleAvAddrMapper.updateByPrimaryKey(eycVehicleAvAddr);
        }
        return eycVehicleAvAddr;
    }

    private boolean isDeclaredField(Class<?> cls, String fieldNme) {
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            if (StrUtil.equals(fieldNme, field.getName())) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public boolean isLogin() {
        return SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
            && !(SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken);
    }

    @Override
    public EycDataConfig searchConfigByTypeAndNme(String type, String name) {
        Condition condition = new Condition(EycDataConfig.class, true, true);
        condition.createCriteria().andEqualTo("dcTypeCode", type).andEqualTo("dcOptionsCn", name);
        List<EycDataConfig> eycDataConfigs = this.eycDataConfigMapper.selectByCondition(condition);
        return CollUtil.isNotEmpty(eycDataConfigs) ? eycDataConfigs.get(0) : null;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchConfigByTypeCde(java.lang.String)
     */
    @Override
    public List<EycDataConfig> searchConfigByTypeCde(String typeCde) {
        Condition condition = new Condition(EycDataConfig.class, true, true);
        condition.createCriteria().andEqualTo("dcTypeCode", typeCde);
        return this.eycDataConfigMapper.selectByCondition(condition);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchConfigByTypeCdes(java.util.List)
     */
    @Override
    public Map<String, List<EycDataConfig>> searchConfigByTypeCdes(List<String> typeCdes) {
        Condition condition = new Condition(EycDataConfig.class, true, true);
        condition.setOrderByClause("IS_FAVOURITE DESC,DATA_CONFIG_ID");
        condition.createCriteria().andIn("dcTypeCode", typeCdes).andEqualTo("disable", Boolean.FALSE);
        List<EycDataConfig> eycDataConfigs = this.eycDataConfigMapper.selectByCondition(condition);
        return CollUtil.isNotEmpty(eycDataConfigs) ? eycDataConfigs.stream().collect(Collectors.groupingBy(EycDataConfig::getDcTypeCode)) : null;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchCountCouponByUserId(java.lang.String)
     */
    @Override
    @ApiOperation(value = "查询优惠券数量")
    public Integer searchCountCouponByUserId(String userId) {
        Map<String, Object> params = new HashMap<String, Object>() {

            private static final long serialVersionUID = 820858501782003002L;
            {
                put(EycConstants.USER_ID, userId);
                put("isUse", Boolean.FALSE);
                put("isExpire", Boolean.FALSE);
                put("typeCde", CouponTypeEnum.COUPON.getCode());
            }

        };
        return this.eycUserCouponMapper.searchCouponCountByUserIdAndType(params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchCouponByUserIdAndType(java.util.Map)
     */
    @Override
    public PageInfo<Map<String, Object>> searchCouponByUserIdAndType(Map<String, Object> params) {
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>();
        pageInfo.setList(this.eycUserCouponMapper.searchCouponByUserIdAndType(params));
        pageInfo.setTotal(this.eycUserCouponMapper.searchCouponCountByUserIdAndType(params));
        pageInfo.setPageNum((int)params.get(EycConstants.PAGE_NUMBER));
        return pageInfo;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchDataConfigs(java.util.Map)
     */
    @Override
    public PageInfo<EycDataConfig> searchDataConfigs(Map<String, Object> params) {
        return this.searchPageInfo(EycDataConfig.class, params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycVehicleAvByPlateNum(java.lang.String)
     */
    @Override
    public EycVehicleAv searchEycVehicleAvByPlateNum(String plateNum) {
        return this.eycVehicleAvMapper.searchEycVehicleAvByPlateNum(plateNum);
    }

    @Override
    public List<EycVehicleMaintQa> searchEycVehicleMaintQa() {
        EycVehicleMaintQa eycVehicleMaintQa = new EycVehicleMaintQa();
        eycVehicleMaintQa.setIsActive(Boolean.TRUE);
        return this.eycVehicleMaintQaMapper.select(null);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchInspStations(java.util.Map)
     */
    @Override
    public PageInfo<EycInspStation> searchInspStations(Map<String, Object> params) {
        return this.searchPageInfo(EycInspStation.class, params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchMatchInspPriceRule(java.util.Map)
     */
    @Override
    public EycInspPriceRule searchMatchInspPriceRule(Map<String, String> params) {
        return this.eycInspPriceRuleMapper.searchMatchInspPriceRule(params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchInspectionRule()
     */
    @Override
    public EycInspRule searchMatchInspRule(Map<String, String> params) {
        return this.eycInspRuleMapper.searchMatchInspRule(params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchOneCouponByUserId(java.lang.String)
     */
    @ApiOperation(value = "查询优惠券")
    @Override
    public EycUserCoupon searchOneCouponByUserId(String userId) {
        return this.eycUserCouponMapper.searchOneCouponByUserId(userId);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchPageConfigByTypeCde(java.lang.String, java.lang.String)
     */
    @Override
    public List<EycPublicInfo> searchPageConfigByTypeCde(String pageCde, String typeCde, String status) {
        Condition condition = new Condition(EycPublicInfo.class, true, true);
        Criteria criteria = condition.createCriteria().andEqualTo("pageCde", pageCde).andEqualTo("type", typeCde);
        if (StrUtil.isNotBlank(status)) {
            criteria.andEqualTo("status", status);
        } else {
            criteria.andNotEqualTo("status", PublishInfoStatusEnum.DELETE.getCode());
        }
        condition.setOrderByClause("CREATE_TIME desc");
        return this.eycPublicInfoMapper.selectByCondition(condition);
    }

    private <T> PageInfo<T> searchPageInfo(Class<T> cls, Map<String, Object> params) {
        PageHelper.startPage((int)params.get(EycConstants.PAGE_NUMBER), (int)params.get(EycConstants.PAGE_SIZE));
        Condition condition = new Condition(cls);
        condition.setOrderByClause("CREATE_TIME desc");
        Criteria criteria = condition.createCriteria();
        try {
            for (Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() != null && isDeclaredField(cls, entry.getKey())) {
                    if (StrUtil.equalsIgnoreCase((String)entry.getValue(), "true") || StrUtil.equalsIgnoreCase((String)entry.getValue(), "false")) {
                        criteria.andEqualTo(entry.getKey(), Boolean.valueOf((String)entry.getValue()));
                    } else if (entry.getValue() instanceof String) {
                        criteria.andLike(entry.getKey(), '%' + (String)entry.getValue() + '%');
                    } else {
                        criteria.andEqualTo(entry.getKey(), entry.getValue());
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return new PageInfo<>(this.getTableMapper(cls).selectByCondition(condition));
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchPublishInfoById(java.lang.String)
     */
    @Override
    public EycPublicInfo searchPublishInfoById(String publicInfoId) {
        return this.eycPublicInfoMapper.selectByPrimaryKey(publicInfoId);
    }

    /**
     * @param typeCdes
     * @return
     */
    @Override
    public List<EycPublicInfo> searchPublishInfoByTypeCdes(List<String> typeCdes) {
        return this.searchPublishInfoByTypeCdes("index", typeCdes, PublishInfoStatusEnum.FILE.getCode());
    }

    /**
     * @param typeCdes
     * @param status
     * @return
     */
    @Override
    public List<EycPublicInfo> searchPublishInfoByTypeCdes(String pageCde, List<String> typeCdes, String status) {
        Condition condition = new Condition(EycPublicInfo.class, true, true);
        condition.createCriteria().andEqualTo("pageCde", pageCde).andIn("type", typeCdes).andEqualTo("status", status);
        condition.setOrderByClause("CREATE_TIME desc");
        return this.eycPublicInfoMapper.selectByCondition(condition);
    }
    
    @Override
    public List<EycAdInfo> searchCarouselPictures(){
        Condition condition = new Condition(EycAdInfo.class, true, true);
        condition.createCriteria().andEqualTo("pageIndex", "AD_PAGE_INDEX001").andNotEqualTo("deleted", 1);
        condition.setOrderByClause("CREATE_TIME desc");
        return this.eycAdInfoMapper.selectByCondition(condition);
    }

    @Override
    public List<EycPublicInfo> searchPublishInfoPageByTypeCde(String typeCde) {
        Condition condition = new Condition(EycPublicInfo.class, true, true);
        condition.selectProperties("pageCde", "pageNme").setDistinct(Boolean.TRUE);
        condition.createCriteria().andEqualTo("type", typeCde);
        condition.setOrderByClause("CREATE_TIME desc");
        return this.eycPublicInfoMapper.selectByCondition(condition);
    }

    /**
     * @see com.chengyu.eyc.service.EycService#totalVehicleValue(java.lang.String)
     */
    @Override
    public BigDecimal sumVehicleAvAmount(String userId) {
        return this.eycVehicleAvMapper.sumVehicleAvAmount(userId);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#updateCouponIsUsed(java.lang.String)
     */
    @Override
    public EycUserCoupon updateCouponIsUsed(String couponId, Boolean isUsed) {
        EycUserCoupon eycUserCoupon = this.eycUserCouponMapper.selectByPrimaryKey(couponId);
        eycUserCoupon.setIsUse(isUsed);
        this.eycUserCouponMapper.updateByPrimaryKey(eycUserCoupon);
        return eycUserCoupon;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#updatehPublishInfoStatus(com.chengyu.eyc.model.EycPublicInfo)
     */
    @Override
    public EycPublicInfo updatehPublishInfoStatus(EycPublicInfo eycPublicInfo) {
        this.eycPublicInfoMapper.updateByPrimaryKey(eycPublicInfo);
        return eycPublicInfo;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchInspStationWorkdays(java.lang.String, java.lang.String)
     */
    @Override
    public List<EycInspStationWorkday> searchInspStationWorkdays(String startDate, String endDate) {
        Condition condition = new Condition(EycInspStationWorkday.class);
        condition.createCriteria().andBetween("dayOffToWd", startDate, endDate).orBetween("wdToDayOff", startDate, endDate);
        return this.eycInspStationWorkdayMapper.selectByCondition(condition);
    }
    
  

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycInspectionStations(java.util.Map)
     */
    @Override
    public List<EycInspStation> searchEycInspectionStations(Map<String, Object> params) {
        return this.eycInspStationMapper.searchEycInspectionStations(params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycInspStationById(java.lang.String)
     */
    @Override
    public EycInspStation searchEycInspStationById(String inspStationId) {
        return this.eycInspStationMapper.selectByPrimaryKey(inspStationId);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycOtherShopById(java.lang.String)
     */
    @Override
    public EycOtherShop searchEycOtherShopById(String poiId) {
        return this.eycOtherShopMapper.selectByPrimaryKey(poiId);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycInspStationById(java.util.Map)
     */
    @Override
    public Integer sumCouponAmountByUserIdAndType(Map<String, Object> params) {
        return this.eycUserCouponMapper.sumCouponAmountByUserIdAndType(params);
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycCouponRuleByTypeCde(java.lang.String)
     */
    @Override
    public List<Map<String, Object>> searchEycCouponRuleByTypeCde(Map<String,Object> params) {
        return this.eycCouponRuleMapper.searchEycCouponRuleByTypeCde(params);
    }
    
    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchEycCouponRuleByTypeCde(java.lang.String)
     */
    @Override
    public List<EycCouponRule> searchVehicleTypeCategoryItemByTypeCde(String typeCde){
        return searchEycCouponCategoryItemByTypeCde(typeCde,"vehicleType","vehicleTypeNme");
    }
    
    /**
     * @see com.chengyu.eyc.service.EycBaseService#searchSubTypeCategoryItemByTypeCde(java.lang.String)
     */
    @Override
    public List<EycCouponRule> searchSubTypeCategoryItemByTypeCde(String typeCde){
        return searchEycCouponCategoryItemByTypeCde(typeCde,"subTypeCde","subTypeNme");
    }

    /**
     * @param typeCde
     * @return
     */
    private List<EycCouponRule> searchEycCouponCategoryItemByTypeCde(String typeCde,String...properties) {
        Condition condition=new Condition(EycCouponRule.class);
        condition.selectProperties(properties);
        condition.setDistinct(Boolean.TRUE);
        condition.createCriteria().andEqualTo("isActive", Boolean.TRUE).andEqualTo("typeCde", typeCde);
        Criteria criteria = condition.createCriteria();
        criteria.andIsNull("expiringDt").orGreaterThanOrEqualTo("expiringDt", new Date());
        condition.and(criteria);
        return this.eycCouponRuleMapper.selectByCondition(condition);
    }
    
    /**
     * @see com.chengyu.eyc.service.EycBaseService#receiveCoupon(com.chengyu.eyc.model.EycCouponRule, java.lang.String)
     */
    @Override
    public EycUserCoupon receiveCoupon(EycCouponRule eycCouponRule, String userId) {
        eycCouponRule = this.eycCouponRuleMapper.selectByPrimaryKey(eycCouponRule.getEycCouponRuleId());
        eycCouponRule.setIssuedQty(eycCouponRule.getIssuedQty() == null ? 1 : eycCouponRule.getIssuedQty() + 1);
        eycCouponRule.setIsActive(!eycCouponRule.getIssuedQty().equals(eycCouponRule.getQty()));
        this.eycCouponRuleMapper.updateByPrimaryKey(eycCouponRule);
        EycUserCoupon eycUserCoupon = new EycUserCoupon();
        BeanUtil.copyProperties(eycCouponRule, eycUserCoupon,"createPerson","updatePerson");
        eycUserCoupon.setUserId(userId);
        eycUserCoupon.setReciicedTime(new Date());
        if (eycCouponRule.getExpiringDt() == null && eycCouponRule.getAfterRec() != null) {
            Date newDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, eycCouponRule.getAfterRec());
            eycUserCoupon.setExpirationDt(newDate);
        }
        eycUserCoupon.setIsUse(Boolean.FALSE);
        this.insertEycUserCoupon(eycUserCoupon);
        return eycUserCoupon;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#receiveCoupons(java.util.List, java.lang.String)
     */
    @Override
    public Boolean receiveCoupons(List<EycCouponRule> eycCouponRules, String userId) {
        eycCouponRules.forEach(item->{
            receiveCoupon(item,userId);
        });
        return Boolean.TRUE;
    }

    /**
     * @see com.chengyu.eyc.service.EycBaseService#hasVerificationAuthority(java.lang.String)
     */
    @Override
    public List<EycVerPerson> hasVerificationAuthority(String userId) {
        Condition condition=new Condition(EycVerPerson.class);
        condition.createCriteria().andEqualTo("userId", userId).andEqualTo("isActive", 1);
        return this.eycVerPersonMapper.selectByCondition(condition);
    }

}
